package com.wedo.mall.activity.user;

import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;

import com.e.library.activity.EActivity;
import com.wedo.mall.R;
import com.wedo.mall.fragment.CollectionGoodsFragment;
import com.wedo.mall.fragment.CollectionShopFragment;

import java.lang.reflect.Field;
import java.util.ArrayList;

import butterknife.BindView;

public class MyCollectionActivity extends EActivity {

    @BindView(R.id.mTabLayout) TabLayout mTabLayout;
    private String[] tabTitle = null;
    private ArrayList<Fragment> fragments;

    @BindView(R.id.mViewPager) ViewPager mViewPager;
    private PagerAdapter pagerAdapter;

    @Override
    protected int layout() {
        return R.layout.activity_my_collection;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        init();
    }

    private void init(){
        setTabLayout();

        fragments = new ArrayList<Fragment>();
        fragments.add(CollectionGoodsFragment.with());
        fragments.add(CollectionShopFragment.with());

        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(pagerAdapter);
        //在设置viewpager页面滑动监听时，创建TabLayout的滑动监听
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));

        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //在选中的顶部标签时，为viewpager设置currentitem
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void setTabLayout(){
        tabTitle = new String[]{
                this.getString(R.string.user_products),
                this.getString(R.string.user_merchant)};

        for (int i=0; i<tabTitle.length; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(tabTitle[i]));
        }
        mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        //设置顶部标签指示条的颜色和选中页面时标签字体颜色
        mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.e_purple));
        mTabLayout.setTabTextColors(Color.GRAY,getResources().getColor(R.color.e_purple));


        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(mTabLayout, 50, 50);
            }
        });
    }

    //利用反射来修改tablayout的长度，最后在mTabLayout.post(new Runnable()中调用
    public void setIndicator (TabLayout tabs,int leftDip,int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

    class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

    }
}
